ALTER  PROC SP_DOWNGRADE_DIEBANK_BINS( @lot_Id INT, @wafer_Id INT )AS
BEGIN

	DECLARE @processStep_Id INT
	DECLARE @baseProduct_Id INT
	DECLARE @BIN_1     	INT
	DECLARE @BIN_2     	INT
	DECLARE @BIN_3     	INT
	DECLARE @BIN_4     	INT
	DECLARE @BIN_5     	INT
	DECLARE @BIN_6     	INT
	DECLARE @BIN_7     	INT
	DECLARE @BIN_8     	INT
	DECLARE @BIN_9     	INT
	DECLARE @BIN_10     	INT
	DECLARE @BIN_11     	INT
	DECLARE @BIN_12     	INT
	DECLARE @BIN_13     	INT
	DECLARE @BIN_14     	INT
	DECLARE @BIN_15     	INT
	DECLARE @count 		INT
	DECLARE @downBin_number	VARCHAR(50)
	DECLARE @downBin_Cursor CURSOR
	
	--Downgrade the bins only if no bins of the Wafer are released to Assembly. This is required because a wafer can be partially scrapped.
	if( (select count(*) from ASSM_RELEASE_TABLE ART, ASSM_RELEASE_LOT_TABLE ARLT WHERE ART.ASSEMBLY_RELEASE_ID = ARLT.ASSEMBLY_RELEASE_ID AND ARLT.LOT_ID = @LOT_ID AND ARLT.WAFER_ID = @WAFER_ID AND ART.STATUS <> 'CANCELLED') = 0 )
	BEGIN
		SELECT @processStep_Id = LT.ProcessStep_Id, @baseProduct_Id = P.P_Parent_Product_Id FROM LOT_Table LT, Product P where LT.ProcessStep_Id = P.ProcessStep_Id AND Lot_Id = @lot_Id
		
		SELECT @BIN_1 = BIN_1, @BIN_2 = Bin_2, @BIN_3 = Bin_3, @BIN_4 = Bin_4, @BIN_5 = Bin_5, @BIN_6 = Bin_6, @BIN_7 = Bin_7, @BIN_8 = Bin_8, @BIN_9 = Bin_9, @BIN_10 = Bin_10, @BIN_11 = Bin_11, @BIN_12 = Bin_12, @BIN_13 = Bin_13, @BIN_14 = Bin_14, @BIN_15 = Bin_15 FROM Informative_Binning WHERE Lot_Id = @lot_Id AND Wafer_Id = @wafer_Id
			
		SELECT @count = COUNT(*) FROM SORT_BIN_RELEASE_TABLE WHERE Lot_Id = @lot_Id AND Wafer_Id = @wafer_Id
		IF ( @count = 0 )	
		BEGIN
			INSERT INTO SORT_BIN_RELEASE_TABLE ( Lot_Id, Wafer_Id ) VALUES ( @lot_Id, @wafer_Id )
		END
		
		IF @BIN_1 > 0
		BEGIN
			SELECT @downBin_number = Downsize_Bin_Number FROM MRP_Binning_Mapping WHERE Base_Product_ID = @baseProduct_Id AND Bin_Number = 'Bin1'
			IF @downBin_number IS NOT NULL AND LEN(LTRIM(RTRIM( @downBin_number ) ) ) > 1
			BEGIN
				IF @downBin_number = 'BIN2'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_1 = 0 ,BIN_1_BALANCE_QTY = 0 ,BIN_2 = ISNULL( BIN_2, 0 ) + @BIN_1, BIN_2_BALANCE_QTY = ISNULL( BIN_2_BALANCE_QTY, BIN_2 ) + @BIN_1, IS_DOWN_GRADE_BIN = 0 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN3'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_1 = 0 ,BIN_1_BALANCE_QTY = 0 ,BIN_3 = ISNULL( BIN_3, 0 ) + @BIN_1, BIN_3_BALANCE_QTY = ISNULL( BIN_3_BALANCE_QTY, BIN_3 ) + @BIN_1, IS_DOWN_GRADE_BIN = 0 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN4'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_1 = 0 ,BIN_1_BALANCE_QTY = 0 ,BIN_4 = ISNULL( BIN_4, 0 ) + @BIN_1, BIN_4_BALANCE_QTY = ISNULL( BIN_4_BALANCE_QTY, BIN_4 ) + @BIN_1, IS_DOWN_GRADE_BIN = 0 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN5'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_1 = 0 ,BIN_1_BALANCE_QTY = 0 ,BIN_5 = ISNULL( BIN_5, 0 ) + @BIN_1, BIN_5_BALANCE_QTY = ISNULL( BIN_5_BALANCE_QTY, BIN_5 ) + @BIN_1, IS_DOWN_GRADE_BIN = 0 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN6'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_1 = 0 ,BIN_1_BALANCE_QTY = 0 ,BIN_6 = ISNULL( BIN_6, 0 ) + @BIN_1, BIN_6_BALANCE_QTY = ISNULL( BIN_6_BALANCE_QTY, BIN_6 ) + @BIN_1, IS_DOWN_GRADE_BIN = 0 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN7'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_1 = 0 ,BIN_1_BALANCE_QTY = 0 ,BIN_7 = ISNULL( BIN_7, 0 ) + @BIN_1, BIN_7_BALANCE_QTY = ISNULL( BIN_7_BALANCE_QTY, BIN_7 ) + @BIN_1, IS_DOWN_GRADE_BIN = 0 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN8'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_1 = 0 ,BIN_1_BALANCE_QTY = 0 ,BIN_8 = ISNULL( BIN_8, 0 ) + @BIN_1, BIN_8_BALANCE_QTY = ISNULL( BIN_8_BALANCE_QTY, BIN_8 ) + @BIN_1, IS_DOWN_GRADE_BIN = 0 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN9'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_1 = 0 ,BIN_1_BALANCE_QTY = 0 ,BIN_9 = ISNULL( BIN_9, 0 ) + @BIN_1, BIN_9_BALANCE_QTY = ISNULL( BIN_9_BALANCE_QTY, BIN_9 ) + @BIN_1, IS_DOWN_GRADE_BIN = 0 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN10'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_1 = 0 ,BIN_1_BALANCE_QTY = 0 ,BIN_10 = ISNULL( BIN_10, 0 ) + @BIN_1, BIN_10_BALANCE_QTY = ISNULL( BIN_10_BALANCE_QTY, BIN_10 ) + @BIN_1, IS_DOWN_GRADE_BIN = 0 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN11'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_1 = 0 ,BIN_1_BALANCE_QTY = 0 ,BIN_11 = ISNULL( BIN_11, 0 ) + @BIN_1, BIN_11_BALANCE_QTY = ISNULL( BIN_11_BALANCE_QTY, BIN_11 ) + @BIN_1, IS_DOWN_GRADE_BIN = 0 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN12'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_1 = 0 ,BIN_1_BALANCE_QTY = 0 ,BIN_12 = ISNULL( BIN_12, 0 ) + @BIN_1, BIN_12_BALANCE_QTY = ISNULL( BIN_12_BALANCE_QTY, BIN_12 ) + @BIN_1, IS_DOWN_GRADE_BIN = 0 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN13'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_1 = 0 ,BIN_1_BALANCE_QTY = 0 ,BIN_13 = ISNULL( BIN_13, 0 ) + @BIN_1, BIN_13_BALANCE_QTY = ISNULL( BIN_13_BALANCE_QTY, BIN_13 ) + @BIN_1, IS_DOWN_GRADE_BIN = 0 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN14'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_1 = 0 ,BIN_1_BALANCE_QTY = 0 ,BIN_14 = ISNULL( BIN_14, 0 ) + @BIN_1, BIN_14_BALANCE_QTY = ISNULL( BIN_14_BALANCE_QTY, BIN_14 ) + @BIN_1, IS_DOWN_GRADE_BIN = 0 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN15'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_1 = 0 ,BIN_1_BALANCE_QTY = 0 ,BIN_15 = ISNULL( BIN_15, 0 ) + @BIN_1, BIN_15_BALANCE_QTY = ISNULL( BIN_15_BALANCE_QTY, BIN_15 ) + @BIN_1, IS_DOWN_GRADE_BIN = 0 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
	
			END
			ELSE
			BEGIN
				UPDATE SORT_BIN_RELEASE_TABLE SET BIN_1 = ISNULL( BIN_1, 0 ) + @BIN_1, Bin_1_Balance_Qty = ISNULL( Bin_1_Balance_Qty, Bin_1 )+ @BIN_1 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
			END
		END
		IF @BIN_2 > 0 
		BEGIN
			SELECT @downBin_number = Downsize_Bin_Number FROM MRP_Binning_Mapping WHERE Base_Product_ID = @baseProduct_Id AND Bin_Number = 'Bin2'
			IF @downBin_number IS NOT NULL AND LEN(LTRIM(RTRIM( @downBin_number ) ) ) > 1
			BEGIN
				IF @downBin_number = 'BIN3'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_2 = 0 ,BIN_2_BALANCE_QTY = 0 ,BIN_3 = ISNULL( BIN_3, 0 ) + @BIN_2, BIN_3_BALANCE_QTY = ISNULL( BIN_3_BALANCE_QTY, BIN_3 ) + @BIN_2 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN4'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_2 = 0 ,BIN_2_BALANCE_QTY = 0 ,BIN_4 = ISNULL( BIN_4, 0 ) + @BIN_2, BIN_4_BALANCE_QTY = ISNULL( BIN_4_BALANCE_QTY, BIN_4 ) + @BIN_2 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN5'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_2 = 0 ,BIN_2_BALANCE_QTY = 0 ,BIN_5 = ISNULL( BIN_5, 0 ) + @BIN_2, BIN_5_BALANCE_QTY = ISNULL( BIN_5_BALANCE_QTY, BIN_5 ) + @BIN_2 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN6'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_2 = 0 ,BIN_2_BALANCE_QTY = 0 ,BIN_6 = ISNULL( BIN_6, 0 ) + @BIN_2, BIN_6_BALANCE_QTY = ISNULL( BIN_6_BALANCE_QTY, BIN_6 ) + @BIN_2 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN7'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_2 = 0 ,BIN_2_BALANCE_QTY = 0 ,BIN_7 = ISNULL( BIN_7, 0 ) + @BIN_2, BIN_7_BALANCE_QTY = ISNULL( BIN_7_BALANCE_QTY, BIN_7 ) + @BIN_2 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN8'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_2 = 0 ,BIN_2_BALANCE_QTY = 0 ,BIN_8 = ISNULL( BIN_8, 0 ) + @BIN_2, BIN_8_BALANCE_QTY = ISNULL( BIN_8_BALANCE_QTY, BIN_8 ) + @BIN_2 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN9'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_2 = 0 ,BIN_2_BALANCE_QTY = 0 ,BIN_9 = ISNULL( BIN_9, 0 ) + @BIN_2, BIN_9_BALANCE_QTY = ISNULL( BIN_9_BALANCE_QTY, BIN_9 ) + @BIN_2 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN10'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_2 = 0 ,BIN_2_BALANCE_QTY = 0 ,BIN_10 = ISNULL( BIN_10, 0 ) + @BIN_2, BIN_10_BALANCE_QTY = ISNULL( BIN_10_BALANCE_QTY, BIN_10 ) + @BIN_2 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN11'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_2 = 0 ,BIN_2_BALANCE_QTY = 0 ,BIN_11 = ISNULL( BIN_11, 0 ) + @BIN_2, BIN_11_BALANCE_QTY = ISNULL( BIN_11_BALANCE_QTY, BIN_11 ) + @BIN_2 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN12'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_2 = 0 ,BIN_2_BALANCE_QTY = 0 ,BIN_12 = ISNULL( BIN_12, 0 ) + @BIN_2, BIN_12_BALANCE_QTY = ISNULL( BIN_12_BALANCE_QTY, BIN_12 ) + @BIN_2 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN13'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_2 = 0 ,BIN_2_BALANCE_QTY = 0 ,BIN_13 = ISNULL( BIN_13, 0 ) + @BIN_2, BIN_13_BALANCE_QTY = ISNULL( BIN_13_BALANCE_QTY, BIN_13 ) + @BIN_2 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN14'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_2 = 0 ,BIN_2_BALANCE_QTY = 0 ,BIN_14 = ISNULL( BIN_14, 0 ) + @BIN_2, BIN_14_BALANCE_QTY = ISNULL( BIN_14_BALANCE_QTY, BIN_14 ) + @BIN_2 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN15'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_2 = 0 ,BIN_2_BALANCE_QTY = 0 ,BIN_15 = ISNULL( BIN_15, 0 ) + @BIN_2, BIN_15_BALANCE_QTY = ISNULL( BIN_15_BALANCE_QTY, BIN_15 ) + @BIN_2 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
			END
			ELSE
			BEGIN
				UPDATE SORT_BIN_RELEASE_TABLE SET BIN_2 = ISNULL( BIN_2, 0 ) + @BIN_2, Bin_2_Balance_Qty = ISNULL( Bin_2_Balance_Qty, Bin_2 )+ @BIN_2  WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
			END
		END
		IF @BIN_3 > 0 
		BEGIN
			SELECT @downBin_number = Downsize_Bin_Number FROM MRP_Binning_Mapping WHERE Base_Product_ID = @baseProduct_Id AND Bin_Number = 'Bin3'
			IF @downBin_number IS NOT NULL AND LEN(LTRIM(RTRIM( @downBin_number ) ) ) > 1
			BEGIN
				
				IF @downBin_number = 'BIN4'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_3 = 0 ,BIN_3_BALANCE_QTY = 0 ,BIN_4 = ISNULL( BIN_4, 0 ) + @BIN_3, BIN_4_BALANCE_QTY = ISNULL( BIN_4_BALANCE_QTY, BIN_4 ) + @BIN_3 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN5'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_3 = 0 ,BIN_3_BALANCE_QTY = 0 ,BIN_5 = ISNULL( BIN_5, 0 ) + @BIN_3, BIN_5_BALANCE_QTY = ISNULL( BIN_5_BALANCE_QTY, BIN_5 ) + @BIN_3 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN6'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_3 = 0 ,BIN_3_BALANCE_QTY = 0 ,BIN_6 = ISNULL( BIN_6, 0 ) + @BIN_3, BIN_6_BALANCE_QTY = ISNULL( BIN_6_BALANCE_QTY, BIN_6 ) + @BIN_3 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN7'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_3 = 0 ,BIN_3_BALANCE_QTY = 0 ,BIN_7 = ISNULL( BIN_7, 0 ) + @BIN_3, BIN_7_BALANCE_QTY = ISNULL( BIN_7_BALANCE_QTY, BIN_7 ) + @BIN_3 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN8'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_3 = 0 ,BIN_3_BALANCE_QTY = 0 ,BIN_8 = ISNULL( BIN_8, 0 ) + @BIN_3, BIN_8_BALANCE_QTY = ISNULL( BIN_8_BALANCE_QTY, BIN_8 ) + @BIN_3 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN9'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_3 = 0 ,BIN_3_BALANCE_QTY = 0 ,BIN_9 = ISNULL( BIN_9, 0 ) + @BIN_3, BIN_9_BALANCE_QTY = ISNULL( BIN_9_BALANCE_QTY, BIN_9 ) + @BIN_3 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN10'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_3 = 0 ,BIN_3_BALANCE_QTY = 0 ,BIN_10 = ISNULL( BIN_10, 0 ) + @BIN_3, BIN_10_BALANCE_QTY = ISNULL( BIN_10_BALANCE_QTY, BIN_10 ) + @BIN_3 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN11'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_3 = 0 ,BIN_3_BALANCE_QTY = 0 ,BIN_11 = ISNULL( BIN_11, 0 ) + @BIN_3, BIN_11_BALANCE_QTY = ISNULL( BIN_11_BALANCE_QTY, BIN_11 ) + @BIN_3 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN12'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_3 = 0 ,BIN_3_BALANCE_QTY = 0 ,BIN_12 = ISNULL( BIN_12, 0 ) + @BIN_3, BIN_12_BALANCE_QTY = ISNULL( BIN_12_BALANCE_QTY, BIN_12 ) + @BIN_3 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN13'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_3 = 0 ,BIN_3_BALANCE_QTY = 0 ,BIN_13 = ISNULL( BIN_13, 0 ) + @BIN_3, BIN_13_BALANCE_QTY = ISNULL( BIN_13_BALANCE_QTY, BIN_13 ) + @BIN_3 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN14'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_3 = 0 ,BIN_3_BALANCE_QTY = 0 ,BIN_14 = ISNULL( BIN_14, 0 ) + @BIN_3, BIN_14_BALANCE_QTY = ISNULL( BIN_14_BALANCE_QTY, BIN_14 ) + @BIN_3 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN15'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_3 = 0 ,BIN_3_BALANCE_QTY = 0 ,BIN_15 = ISNULL( BIN_15, 0 ) + @BIN_3, BIN_15_BALANCE_QTY = ISNULL( BIN_15_BALANCE_QTY, BIN_15 ) + @BIN_3 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
			END
			ELSE
			BEGIN
				UPDATE SORT_BIN_RELEASE_TABLE SET BIN_3 = ISNULL( BIN_3, 0 ) + @BIN_3, Bin_3_Balance_Qty = ISNULL( Bin_3_Balance_Qty, Bin_3 )+ @BIN_3  WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
			END		
		END
		IF @BIN_4 > 0 
		BEGIN
			SELECT @downBin_number = Downsize_Bin_Number FROM MRP_Binning_Mapping WHERE Base_Product_ID = @baseProduct_Id AND Bin_Number = 'Bin4'
			IF @downBin_number IS NOT NULL AND LEN(LTRIM(RTRIM( @downBin_number ) ) ) > 1
			BEGIN
				
				IF @downBin_number = 'BIN5'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_4 = 0 ,BIN_4_BALANCE_QTY = 0 ,BIN_5 = ISNULL( BIN_5, 0 ) + @BIN_4, BIN_5_BALANCE_QTY = ISNULL( BIN_5_BALANCE_QTY, BIN_5 ) + @BIN_4 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN6'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_4 = 0 ,BIN_4_BALANCE_QTY = 0 ,BIN_6 = ISNULL( BIN_6, 0 ) + @BIN_4, BIN_6_BALANCE_QTY = ISNULL( BIN_6_BALANCE_QTY, BIN_6 ) + @BIN_4 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN7'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_4 = 0 ,BIN_4_BALANCE_QTY = 0 ,BIN_7 = ISNULL( BIN_7, 0 ) + @BIN_4, BIN_7_BALANCE_QTY = ISNULL( BIN_7_BALANCE_QTY, BIN_7 ) + @BIN_4 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN8'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_4 = 0 ,BIN_4_BALANCE_QTY = 0 ,BIN_8 = ISNULL( BIN_8, 0 ) + @BIN_4, BIN_8_BALANCE_QTY = ISNULL( BIN_8_BALANCE_QTY, BIN_8 ) + @BIN_4 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN9'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_4 = 0 ,BIN_4_BALANCE_QTY = 0 ,BIN_9 = ISNULL( BIN_9, 0 ) + @BIN_4, BIN_9_BALANCE_QTY = ISNULL( BIN_9_BALANCE_QTY, BIN_9 ) + @BIN_4 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN10'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_4 = 0 ,BIN_4_BALANCE_QTY = 0 ,BIN_10 = ISNULL( BIN_10, 0 ) + @BIN_4, BIN_10_BALANCE_QTY = ISNULL( BIN_10_BALANCE_QTY, BIN_10 ) + @BIN_4 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN11'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_4 = 0 ,BIN_4_BALANCE_QTY = 0 ,BIN_11 = ISNULL( BIN_11, 0 ) + @BIN_4, BIN_11_BALANCE_QTY = ISNULL( BIN_11_BALANCE_QTY, BIN_11 ) + @BIN_4 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN12'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_4 = 0 ,BIN_4_BALANCE_QTY = 0 ,BIN_12 = ISNULL( BIN_12, 0 ) + @BIN_4, BIN_12_BALANCE_QTY = ISNULL( BIN_12_BALANCE_QTY, BIN_12 ) + @BIN_4 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN13'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_4 = 0 ,BIN_4_BALANCE_QTY = 0 ,BIN_13 = ISNULL( BIN_13, 0 ) + @BIN_4, BIN_13_BALANCE_QTY = ISNULL( BIN_13_BALANCE_QTY, BIN_13 ) + @BIN_4 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN14'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_4 = 0 ,BIN_4_BALANCE_QTY = 0 ,BIN_14 = ISNULL( BIN_14, 0 ) + @BIN_4, BIN_14_BALANCE_QTY = ISNULL( BIN_14_BALANCE_QTY, BIN_14 ) + @BIN_4 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN15'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_4 = 0 ,BIN_4_BALANCE_QTY = 0 ,BIN_15 = ISNULL( BIN_15, 0 ) + @BIN_4, BIN_15_BALANCE_QTY = ISNULL( BIN_15_BALANCE_QTY, BIN_15 ) + @BIN_4 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
			END
			ELSE
			BEGIN
				UPDATE SORT_BIN_RELEASE_TABLE SET BIN_4 = ISNULL( BIN_4, 0 ) + @BIN_4, Bin_4_Balance_Qty = ISNULL( Bin_4_Balance_Qty, Bin_4 )+ @BIN_4  WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
			END	
		END
		IF @BIN_5 > 0 
		BEGIN
			SELECT @downBin_number = Downsize_Bin_Number FROM MRP_Binning_Mapping WHERE Base_Product_ID = @baseProduct_Id AND Bin_Number = 'Bin5'
			IF @downBin_number IS NOT NULL AND LEN(LTRIM(RTRIM( @downBin_number ) ) ) > 1
			BEGIN
				IF @downBin_number = 'BIN6'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_5 = 0 ,BIN_5_BALANCE_QTY = 0 ,BIN_6 = ISNULL( BIN_6, 0 ) + @BIN_5, BIN_6_BALANCE_QTY = ISNULL( BIN_6_BALANCE_QTY, BIN_6 ) + @BIN_5 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN7'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_5 = 0 ,BIN_5_BALANCE_QTY = 0 ,BIN_7 = ISNULL( BIN_7, 0 ) + @BIN_5, BIN_7_BALANCE_QTY = ISNULL( BIN_7_BALANCE_QTY, BIN_7 ) + @BIN_5 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN8'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_5 = 0 ,BIN_5_BALANCE_QTY = 0 ,BIN_8 = ISNULL( BIN_8, 0 ) + @BIN_5, BIN_8_BALANCE_QTY = ISNULL( BIN_8_BALANCE_QTY, BIN_8 ) + @BIN_5 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN9'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_5 = 0 ,BIN_5_BALANCE_QTY = 0 ,BIN_9 = ISNULL( BIN_9, 0 ) + @BIN_5, BIN_9_BALANCE_QTY = ISNULL( BIN_9_BALANCE_QTY, BIN_9 ) + @BIN_5 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN10'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_5 = 0 ,BIN_5_BALANCE_QTY = 0 ,BIN_10 = ISNULL( BIN_10, 0 ) + @BIN_5, BIN_10_BALANCE_QTY = ISNULL( BIN_10_BALANCE_QTY, BIN_10 ) + @BIN_5 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN11'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_5 = 0 ,BIN_5_BALANCE_QTY = 0 ,BIN_11 = ISNULL( BIN_11, 0 ) + @BIN_5, BIN_11_BALANCE_QTY = ISNULL( BIN_11_BALANCE_QTY, BIN_11 ) + @BIN_5 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN12'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_5 = 0 ,BIN_5_BALANCE_QTY = 0 ,BIN_12 = ISNULL( BIN_12, 0 ) + @BIN_5, BIN_12_BALANCE_QTY = ISNULL( BIN_12_BALANCE_QTY, BIN_12 ) + @BIN_5 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN13'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_5 = 0 ,BIN_5_BALANCE_QTY = 0 ,BIN_13 = ISNULL( BIN_13, 0 ) + @BIN_5, BIN_13_BALANCE_QTY = ISNULL( BIN_13_BALANCE_QTY, BIN_13 ) + @BIN_5 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN14'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_5 = 0 ,BIN_5_BALANCE_QTY = 0 ,BIN_14 = ISNULL( BIN_14, 0 ) + @BIN_5, BIN_14_BALANCE_QTY = ISNULL( BIN_14_BALANCE_QTY, BIN_14 ) + @BIN_5 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN15'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_5 = 0 ,BIN_5_BALANCE_QTY = 0 ,BIN_15 = ISNULL( BIN_15, 0 ) + @BIN_5, BIN_15_BALANCE_QTY = ISNULL( BIN_15_BALANCE_QTY, BIN_15 ) + @BIN_5 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
			END
			ELSE
			BEGIN
				UPDATE SORT_BIN_RELEASE_TABLE SET BIN_5 = ISNULL( BIN_5, 0 ) + @BIN_5, Bin_5_Balance_Qty = ISNULL( Bin_5_Balance_Qty, Bin_5 )+ @BIN_5  WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
			END
		END
		IF @BIN_6 > 0 
		BEGIN
			SELECT @downBin_number = Downsize_Bin_Number FROM MRP_Binning_Mapping WHERE Base_Product_ID = @baseProduct_Id AND Bin_Number = 'Bin6'
			IF @downBin_number IS NOT NULL AND LEN(LTRIM(RTRIM( @downBin_number ) ) ) > 1
			BEGIN
				IF @downBin_number = 'BIN7'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_6 = 0 ,BIN_6_BALANCE_QTY = 0 ,BIN_7 = ISNULL( BIN_7, 0 ) + @BIN_6, BIN_7_BALANCE_QTY = ISNULL( BIN_7_BALANCE_QTY, BIN_7 ) + @BIN_6 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN8'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET  BIN_6 = 0 ,BIN_6_BALANCE_QTY = 0 ,BIN_8 = ISNULL( BIN_8, 0 ) + @BIN_6, BIN_8_BALANCE_QTY = ISNULL( BIN_8_BALANCE_QTY, BIN_8 ) + @BIN_6 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN9'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET  BIN_6 = 0 ,BIN_6_BALANCE_QTY = 0 ,BIN_9 = ISNULL( BIN_9, 0 ) + @BIN_6, BIN_9_BALANCE_QTY = ISNULL( BIN_9_BALANCE_QTY, BIN_9 ) + @BIN_6 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN10'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET  BIN_6 = 0 ,BIN_6_BALANCE_QTY = 0 ,BIN_10 = ISNULL( BIN_10, 0 ) + @BIN_6, BIN_10_BALANCE_QTY = ISNULL( BIN_10_BALANCE_QTY, BIN_10 ) + @BIN_6 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN11'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET  BIN_6 = 0 ,BIN_6_BALANCE_QTY = 0 ,BIN_11 = ISNULL( BIN_11, 0 ) + @BIN_6, BIN_11_BALANCE_QTY = ISNULL( BIN_11_BALANCE_QTY, BIN_11 ) + @BIN_6 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN12'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET  BIN_6 = 0 ,BIN_6_BALANCE_QTY = 0 ,BIN_12 = ISNULL( BIN_12, 0 ) + @BIN_6, BIN_12_BALANCE_QTY = ISNULL( BIN_12_BALANCE_QTY, BIN_12 ) + @BIN_6 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN13'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET  BIN_6 = 0 ,BIN_6_BALANCE_QTY = 0 ,BIN_13 = ISNULL( BIN_13, 0 ) + @BIN_6, BIN_13_BALANCE_QTY = ISNULL( BIN_13_BALANCE_QTY, BIN_13 ) + @BIN_6 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN14'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET  BIN_6 = 0 ,BIN_6_BALANCE_QTY = 0 ,BIN_14 = ISNULL( BIN_14, 0 ) + @BIN_6, BIN_14_BALANCE_QTY = ISNULL( BIN_14_BALANCE_QTY, BIN_14 ) + @BIN_6 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN15'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET  BIN_6 = 0 ,BIN_6_BALANCE_QTY = 0 ,BIN_15 = ISNULL( BIN_15, 0 ) + @BIN_6, BIN_15_BALANCE_QTY = ISNULL( BIN_15_BALANCE_QTY, BIN_15 ) + @BIN_6 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
			END
			ELSE
			BEGIN
				UPDATE SORT_BIN_RELEASE_TABLE SET BIN_6 = ISNULL( BIN_6, 0 ) + @BIN_6, Bin_6_Balance_Qty = ISNULL( Bin_6_Balance_Qty, Bin_6 )+ @BIN_6  WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
			END
		END
		IF @BIN_7 > 0 
		BEGIN
			SELECT @downBin_number = Downsize_Bin_Number FROM MRP_Binning_Mapping WHERE Base_Product_ID = @baseProduct_Id AND Bin_Number = 'Bin7'
			IF @downBin_number IS NOT NULL AND LEN(LTRIM(RTRIM( @downBin_number ) ) ) > 1
			BEGIN
				
				IF @downBin_number = 'BIN8'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET  BIN_7 = 0 ,BIN_7_BALANCE_QTY = 0 ,BIN_8 = ISNULL( BIN_8, 0 ) + @BIN_7, BIN_8_BALANCE_QTY = ISNULL( BIN_8_BALANCE_QTY, BIN_8 ) + @BIN_7 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN9'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_7 = 0 ,BIN_7_BALANCE_QTY = 0 ,BIN_9 = ISNULL( BIN_9, 0 ) + @BIN_7, BIN_9_BALANCE_QTY = ISNULL( BIN_9_BALANCE_QTY, BIN_9 ) + @BIN_7 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN10'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_7 = 0 ,BIN_7_BALANCE_QTY = 0 ,BIN_10 = ISNULL( BIN_10, 0 ) + @BIN_7, BIN_10_BALANCE_QTY = ISNULL( BIN_10_BALANCE_QTY, BIN_10 ) + @BIN_7 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN11'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_7 = 0 ,BIN_7_BALANCE_QTY = 0 ,BIN_11 = ISNULL( BIN_11, 0 ) + @BIN_7, BIN_11_BALANCE_QTY = ISNULL( BIN_11_BALANCE_QTY, BIN_11 ) + @BIN_7 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN12'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_7 = 0 ,BIN_7_BALANCE_QTY = 0 ,BIN_12 = ISNULL( BIN_12, 0 ) + @BIN_7, BIN_12_BALANCE_QTY = ISNULL( BIN_12_BALANCE_QTY, BIN_12 ) + @BIN_7 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN13'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_7 = 0 ,BIN_7_BALANCE_QTY = 0 ,BIN_13 = ISNULL( BIN_13, 0 ) + @BIN_7, BIN_13_BALANCE_QTY = ISNULL( BIN_13_BALANCE_QTY, BIN_13 ) + @BIN_7 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN14'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_7 = 0 ,BIN_7_BALANCE_QTY = 0 ,BIN_14 = ISNULL( BIN_14, 0 ) + @BIN_7, BIN_14_BALANCE_QTY = ISNULL( BIN_14_BALANCE_QTY, BIN_14 ) + @BIN_7 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN15'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_7 = 0 ,BIN_7_BALANCE_QTY = 0 ,BIN_15 = ISNULL( BIN_15, 0 ) + @BIN_7, BIN_15_BALANCE_QTY = ISNULL( BIN_15_BALANCE_QTY, BIN_15 ) + @BIN_7 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
			END
			ELSE
			BEGIN
				UPDATE SORT_BIN_RELEASE_TABLE SET BIN_7 = ISNULL( BIN_7, 0 ) + @BIN_7, Bin_7_Balance_Qty = ISNULL( Bin_7_Balance_Qty, Bin_7 )+ @BIN_7  WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
			END
		END
		IF @BIN_8 > 0 
		BEGIN
			SELECT @downBin_number = Downsize_Bin_Number FROM MRP_Binning_Mapping WHERE Base_Product_ID = @baseProduct_Id AND Bin_Number = 'Bin8'
			IF @downBin_number IS NOT NULL AND LEN(LTRIM(RTRIM( @downBin_number ) ) ) > 1
			BEGIN
				
				IF @downBin_number = 'BIN9'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_8 = 0 ,BIN_8_BALANCE_QTY = 0 ,BIN_9 = ISNULL( BIN_9, 0 ) + @BIN_8, BIN_9_BALANCE_QTY = ISNULL( BIN_9_BALANCE_QTY, BIN_9 ) + @BIN_8 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN10'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_8 = 0 ,BIN_8_BALANCE_QTY = 0 ,BIN_10 = ISNULL( BIN_10, 0 ) + @BIN_8, BIN_10_BALANCE_QTY = ISNULL( BIN_10_BALANCE_QTY, BIN_10 ) + @BIN_8 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN11'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_8 = 0 ,BIN_8_BALANCE_QTY = 0 ,BIN_11 = ISNULL( BIN_11, 0 ) + @BIN_8, BIN_11_BALANCE_QTY = ISNULL( BIN_11_BALANCE_QTY, BIN_11 ) + @BIN_8 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN12'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_8 = 0 ,BIN_8_BALANCE_QTY = 0 ,BIN_12 = ISNULL( BIN_12, 0 ) + @BIN_8, BIN_12_BALANCE_QTY = ISNULL( BIN_12_BALANCE_QTY, BIN_12 ) + @BIN_8 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN13'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_8 = 0 ,BIN_8_BALANCE_QTY = 0 ,BIN_13 = ISNULL( BIN_13, 0 ) + @BIN_8, BIN_13_BALANCE_QTY = ISNULL( BIN_13_BALANCE_QTY, BIN_13 ) + @BIN_8 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN14'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_8 = 0 ,BIN_8_BALANCE_QTY = 0 ,BIN_14 = ISNULL( BIN_14, 0 ) + @BIN_8, BIN_14_BALANCE_QTY = ISNULL( BIN_14_BALANCE_QTY, BIN_14 ) + @BIN_8 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN15'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_8 = 0 ,BIN_8_BALANCE_QTY = 0 ,BIN_15 = ISNULL( BIN_15, 0 ) + @BIN_8, BIN_15_BALANCE_QTY = ISNULL( BIN_15_BALANCE_QTY, BIN_15 ) + @BIN_8 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
			END
			ELSE
			BEGIN
				UPDATE SORT_BIN_RELEASE_TABLE SET BIN_8 = ISNULL( BIN_8, 0 ) + @BIN_8, Bin_8_Balance_Qty = ISNULL( Bin_8_Balance_Qty, Bin_8 )+ @BIN_8  WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
			END
		END
		IF @BIN_9 > 0 
		BEGIN
			SELECT @downBin_number = Downsize_Bin_Number FROM MRP_Binning_Mapping WHERE Base_Product_ID = @baseProduct_Id AND Bin_Number = 'Bin9'
			IF @downBin_number IS NOT NULL AND LEN(LTRIM(RTRIM( @downBin_number ) ) ) > 1
			BEGIN
				
				IF @downBin_number = 'BIN10'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_9 = 0 ,BIN_9_BALANCE_QTY = 0 ,BIN_10 = ISNULL( BIN_10, 0 ) + @BIN_9, BIN_10_BALANCE_QTY = ISNULL( BIN_10_BALANCE_QTY, BIN_10 ) + @BIN_9 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN11'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_9 = 0 ,BIN_9_BALANCE_QTY = 0 ,BIN_11 = ISNULL( BIN_11, 0 ) + @BIN_9, BIN_11_BALANCE_QTY = ISNULL( BIN_11_BALANCE_QTY, BIN_11 ) + @BIN_9 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN12'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_9 = 0 ,BIN_9_BALANCE_QTY = 0 ,BIN_12 = ISNULL( BIN_12, 0 ) + @BIN_9, BIN_12_BALANCE_QTY = ISNULL( BIN_12_BALANCE_QTY, BIN_12 ) + @BIN_9 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN13'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_9 = 0 ,BIN_9_BALANCE_QTY = 0 ,BIN_13 = ISNULL( BIN_13, 0 ) + @BIN_9, BIN_13_BALANCE_QTY = ISNULL( BIN_13_BALANCE_QTY, BIN_13 ) + @BIN_9 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN14'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_9 = 0 ,BIN_9_BALANCE_QTY = 0 ,BIN_14 = ISNULL( BIN_14, 0 ) + @BIN_9, BIN_14_BALANCE_QTY = ISNULL( BIN_14_BALANCE_QTY, BIN_14 ) + @BIN_9 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN15'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_9 = 0 ,BIN_9_BALANCE_QTY = 0 ,BIN_15 = ISNULL( BIN_15, 0 ) + @BIN_9, BIN_15_BALANCE_QTY = ISNULL( BIN_15_BALANCE_QTY, BIN_15 ) + @BIN_9 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
			END
			ELSE
			BEGIN
				UPDATE SORT_BIN_RELEASE_TABLE SET BIN_9 = ISNULL( BIN_9, 0 ) + @BIN_9, Bin_9_Balance_Qty = ISNULL( Bin_9_Balance_Qty, Bin_9 )+ @BIN_9  WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
			END
		END
		IF @BIN_10 > 0 
		BEGIN
			SELECT @downBin_number = Downsize_Bin_Number FROM MRP_Binning_Mapping WHERE Base_Product_ID = @baseProduct_Id AND Bin_Number = 'Bin10'
			IF @downBin_number IS NOT NULL AND LEN(LTRIM(RTRIM( @downBin_number ) ) ) > 1
			BEGIN
				
				IF @downBin_number = 'BIN11'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_10 = 0 ,BIN_10_BALANCE_QTY = 0 ,BIN_11 = ISNULL( BIN_11, 0 ) + @BIN_10, BIN_11_BALANCE_QTY = ISNULL( BIN_11_BALANCE_QTY, BIN_11 ) + @BIN_10 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN12'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_10 = 0 ,BIN_10_BALANCE_QTY = 0 ,BIN_12 = ISNULL( BIN_12, 0 ) + @BIN_10, BIN_12_BALANCE_QTY = ISNULL( BIN_12_BALANCE_QTY, BIN_12 ) + @BIN_10 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN13'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_10 = 0 ,BIN_10_BALANCE_QTY = 0 ,BIN_13 = ISNULL( BIN_13, 0 ) + @BIN_10, BIN_13_BALANCE_QTY = ISNULL( BIN_13_BALANCE_QTY, BIN_13 ) + @BIN_10 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN14'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_10 = 0 ,BIN_10_BALANCE_QTY = 0 ,BIN_14 = ISNULL( BIN_14, 0 ) + @BIN_10, BIN_14_BALANCE_QTY = ISNULL( BIN_14_BALANCE_QTY, BIN_14 ) + @BIN_10 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN15'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_10 = 0 ,BIN_10_BALANCE_QTY = 0 ,BIN_15 = ISNULL( BIN_15, 0 ) + @BIN_10, BIN_15_BALANCE_QTY = ISNULL( BIN_15_BALANCE_QTY, BIN_15 ) + @BIN_10 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
			END
			ELSE
			BEGIN
				UPDATE SORT_BIN_RELEASE_TABLE SET BIN_10 = ISNULL( BIN_10, 0 ) + @BIN_10, Bin_10_Balance_Qty = ISNULL( Bin_10_Balance_Qty, Bin_10 )+ @BIN_10  WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
			END
		END
		IF @BIN_11 > 0 
		BEGIN
			SELECT @downBin_number = Downsize_Bin_Number FROM MRP_Binning_Mapping WHERE Base_Product_ID = @baseProduct_Id AND Bin_Number = 'Bin11'
			IF @downBin_number IS NOT NULL AND LEN(LTRIM(RTRIM( @downBin_number ) ) ) > 1
			BEGIN
				IF @downBin_number = 'BIN12'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_11 = 0 ,BIN_11_BALANCE_QTY = 0 ,BIN_12 = ISNULL( BIN_12, 0 ) + @BIN_11, BIN_12_BALANCE_QTY = ISNULL( BIN_12_BALANCE_QTY, BIN_12 ) + @BIN_11 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN13'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_11 = 0 ,BIN_11_BALANCE_QTY = 0 ,BIN_13 = ISNULL( BIN_13, 0 ) + @BIN_11, BIN_13_BALANCE_QTY = ISNULL( BIN_13_BALANCE_QTY, BIN_13 ) + @BIN_11 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN14'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_11 = 0 ,BIN_11_BALANCE_QTY = 0 ,BIN_14 = ISNULL( BIN_14, 0 ) + @BIN_11, BIN_14_BALANCE_QTY = ISNULL( BIN_14_BALANCE_QTY, BIN_14 ) + @BIN_11 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN15'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_11 = 0 ,BIN_11_BALANCE_QTY = 0 ,BIN_15 = ISNULL( BIN_15, 0 ) + @BIN_11, BIN_15_BALANCE_QTY = ISNULL( BIN_15_BALANCE_QTY, BIN_15 ) + @BIN_11 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
			END
			ELSE
			BEGIN
				UPDATE SORT_BIN_RELEASE_TABLE SET BIN_11 = ISNULL( BIN_11, 0 ) + @BIN_11, Bin_11_Balance_Qty = ISNULL( Bin_11_Balance_Qty, Bin_11 )+ @BIN_11  WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
			END
		END
		IF @BIN_12 > 0 
		BEGIN
			SELECT @downBin_number = Downsize_Bin_Number FROM MRP_Binning_Mapping WHERE Base_Product_ID = @baseProduct_Id AND Bin_Number = 'Bin12'
			IF @downBin_number IS NOT NULL AND LEN(LTRIM(RTRIM( @downBin_number ) ) ) > 1
			BEGIN
				
				IF @downBin_number = 'BIN13'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_12 = 0 ,BIN_12_BALANCE_QTY = 0 ,BIN_13 = ISNULL( BIN_13, 0 ) + @BIN_12, BIN_13_BALANCE_QTY = ISNULL( BIN_13_BALANCE_QTY, BIN_13 ) + @BIN_12 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN14'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_12 = 0 ,BIN_12_BALANCE_QTY = 0 ,BIN_14 = ISNULL( BIN_14, 0 ) + @BIN_12, BIN_14_BALANCE_QTY = ISNULL( BIN_14_BALANCE_QTY, BIN_14 ) + @BIN_12 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN15'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_12 = 0 ,BIN_12_BALANCE_QTY = 0 ,BIN_15 = ISNULL( BIN_15, 0 ) + @BIN_12, BIN_15_BALANCE_QTY = ISNULL( BIN_15_BALANCE_QTY, BIN_15 ) + @BIN_12 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
			END
			ELSE
			BEGIN
				UPDATE SORT_BIN_RELEASE_TABLE SET BIN_12 = ISNULL( BIN_12, 0 ) + @BIN_12, Bin_12_Balance_Qty = ISNULL( Bin_12_Balance_Qty, Bin_12 )+ @BIN_12  WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
			END
		END
		IF @BIN_13 > 0 
		BEGIN
			SELECT @downBin_number = Downsize_Bin_Number FROM MRP_Binning_Mapping WHERE Base_Product_ID = @baseProduct_Id AND Bin_Number = 'Bin13'
			IF @downBin_number IS NOT NULL AND LEN(LTRIM(RTRIM( @downBin_number ) ) ) > 1
			BEGIN
				
				IF @downBin_number = 'BIN14'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_13 = 0 ,BIN_13_BALANCE_QTY = 0 ,BIN_14 = ISNULL( BIN_14, 0 ) + @BIN_13, BIN_14_BALANCE_QTY = ISNULL( BIN_14_BALANCE_QTY, BIN_14 ) + @BIN_13 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
				IF @downBin_number = 'BIN15'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_13 = 0 ,BIN_13_BALANCE_QTY = 0 ,BIN_15 = ISNULL( BIN_15, 0 ) + @BIN_13, BIN_15_BALANCE_QTY = ISNULL( BIN_15_BALANCE_QTY, BIN_15 ) + @BIN_13 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
			END
			ELSE
			BEGIN
				UPDATE SORT_BIN_RELEASE_TABLE SET BIN_13 = ISNULL( BIN_13, 0 ) + @BIN_13, Bin_13_Balance_Qty = ISNULL( Bin_13_Balance_Qty, Bin_13 )+ @BIN_13  WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
			END
		END
		IF @BIN_14 > 0 
		BEGIN
			SELECT @downBin_number = Downsize_Bin_Number FROM MRP_Binning_Mapping WHERE Base_Product_ID = @baseProduct_Id AND Bin_Number = 'Bin14'
			IF @downBin_number IS NOT NULL AND LEN(LTRIM(RTRIM( @downBin_number ) ) ) > 1
			BEGIN
				IF @downBin_number = 'BIN15'
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET BIN_14 = 0 ,BIN_14_BALANCE_QTY = 0 ,BIN_15 = ISNULL( BIN_15, 0 ) + @BIN_14, BIN_15_BALANCE_QTY = ISNULL( BIN_15_BALANCE_QTY, BIN_15 ) + @BIN_14 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
			END
			ELSE
			BEGIN
				UPDATE SORT_BIN_RELEASE_TABLE SET BIN_14 = ISNULL( BIN_14, 0 ) + @BIN_14, Bin_14_Balance_Qty = ISNULL( Bin_14_Balance_Qty, Bin_14 )+ @BIN_14  WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
			END
		END
		IF @BIN_15 > 0 
		BEGIN
			SELECT @downBin_number = Downsize_Bin_Number FROM MRP_Binning_Mapping WHERE Base_Product_ID = @baseProduct_Id AND Bin_Number = 'Bin15'
			IF @downBin_number IS NOT NULL AND LEN(LTRIM(RTRIM( @downBin_number ) ) ) > 1
			BEGIN
				UPDATE SORT_BIN_RELEASE_TABLE SET LOT_ID = 'LOTID' WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
			END
			ELSE
			BEGIN
				UPDATE SORT_BIN_RELEASE_TABLE SET BIN_15 = ISNULL( BIN_15, 0 ) + @BIN_15, Bin_15_Balance_Qty = ISNULL( Bin_15_Balance_Qty, Bin_15 )+ @BIN_15  WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
			END
		END
	END
		
END